﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmdoituong : Form
    {
        public frmdoituong()
        {
            InitializeComponent();
            TV_Tonghop.Themcot("MA_DT", "Mã Đối Tuợng", dt_DoiTuong, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("DOI_TUONG", "Đối Tuợng", dt_DoiTuong, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("TI_LE_DONG", "Tỉ Lệ Đóng", dt_DoiTuong, DataGridViewAutoSizeColumnMode.Fill);
        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

       private void frmdoituong_Load(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienThiDoiTuong", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dt_DoiTuong.DataSource = dt;// do du lieu vao DataGridView dt_DoiTuong
        }

       private void bt_Them_Click(object sender, EventArgs e)
       {
           SqlConnection ketnoi = getconnect();
           ketnoi.Open();
           SqlCommand cmd = new SqlCommand("ThemDoiTuong", ketnoi);
           cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
           cmd.Parameters.Add("@maDT", SqlDbType.Char).Value = cb_maDT.Text;
           cmd.Parameters.Add("@doituong", SqlDbType.NVarChar).Value = DT.Text;
           cmd.Parameters.Add("@tiledong", SqlDbType.Money).Value = Convert.ToDouble( TiLeDong.Text);
           cmd.ExecuteNonQuery();
           frmdoituong_Load(sender, e);
       }

       private void bt_Sua_Click(object sender, EventArgs e)
       {
           SqlConnection ketnoi = getconnect();
           ketnoi.Open();
           SqlCommand cmd = new SqlCommand("CapNhatDoiTuong", ketnoi);
           cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
           cmd.Parameters.Add("@maDT", SqlDbType.Char).Value = cb_maDT.Text;
           cmd.Parameters.Add("@doituong", SqlDbType.NVarChar).Value = DT.Text;
           cmd.Parameters.Add("@tiledong", SqlDbType.Money).Value = Convert.ToDouble( TiLeDong.Text);
           cmd.ExecuteNonQuery();
           frmdoituong_Load(sender, e);
       }

       private void bt_Xoa_Click(object sender, EventArgs e)
       {
           SqlConnection ketnoi = getconnect();
           ketnoi.Open();
           SqlCommand cmd = new SqlCommand("XoaDoiTuong", ketnoi);
           cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
           cmd.Parameters.Add("@maDT", SqlDbType.Char).Value = cb_maDT.Text;
           cmd.ExecuteNonQuery();
           frmdoituong_Load(sender, e);
       }


       int dong;
       private void dt_DoiTuong_RowEnter(object sender, DataGridViewCellEventArgs e)
       {
           dong = e.RowIndex;
           cb_maDT.Text = dt_DoiTuong.Rows[dong].Cells[0].Value.ToString();
           DT.Text = dt_DoiTuong.Rows[dong].Cells[1].Value.ToString();
           TiLeDong.Text = dt_DoiTuong.Rows[dong].Cells[2].Value.ToString();
       }

       




       
       


        }
     }
